/**
 * Created by lesonli on 2016/11/17.
 */
import * as types from '../types'
import api from '../../api/modules/config'
import {
    getList,
    addOrder,
    updateOrder,
    switchState
} from '../../api/modules/order'

const state = {
    order_list: [],
    total: 0,
    showLoading: false,
    state_list: [{id:0,name:'待审'},{id:1,name:'审核通过'},{id:2,name:'审核未通过'},{id:3,name:'冻结'}],
    type_list: [{id:0,name:'普通'},{id:1,name:'异地线下'},{id:2,name:'纯线下课'},{id:3,name:'线上产品'}]
}

// getters
// actions
const actions = {
    get_order_list({
            commit
        }, param) {
        commit(types.ORDER_SHOW_LOADING, param);
        getList(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.ORDER_LIST_LOADED, res.data.msg);
            }
        });
    },
    clear_order({ commit }){
        commit(types.ORDER_CLEAR);
    },
    add_order({
            commit
        }, param) {
        commit(types.ORDER_SHOW_LOADING, param);
        addOrder(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.ORDER_ADDED, { result: res.data.msg, data: param });
                if (param._fn)
                    param._fn();
            }
        });
    },
    update_order({
            commit
        }, param) {
        commit(types.ORDER_SHOW_LOADING, param);
        updateOrder(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.ORDER_UPDATED, param);
                if (param._fn)
                    param._fn();
            }
        });
    },
    delete_order({
            commit
        }, param) {
        commit(types.ORDER_SHOW_LOADING, param);
        switchState(param.id, -1).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.ORDER_DELETED, param);
                if (param._fn)
                    param._fn();
            }
        });
    },
    update_order_state({
            commit
        }, param) {
        commit(types.ORDER_SHOW_LOADING, param);
        switchState(param.id, param.value).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.ORDER_UPDATED, param);
                if (param._fn)
                    param._fn();
            }
        });
    }
}
// mutations
const mutations = {
    [types.ORDER_SHOW_LOADING](state, param) {
        state.showLoading = true;
    },
    [types.ORDER_CLEAR](state) {
        state.order_list = [];
    },
    [types.ORDER_LIST_LOADED](state, param) {
        // state.order_list = param.list;
        state.order_list = param;
        state.total = param.total;
        state.showLoading = false;
    },
    [types.ORDER_ADDED](state, param) {

        var data = {};
        for(var p in param.data)
        {
            data[p] = param.data[p];
        }
        data.guid = param.result.guid;
        data.id = param.result.id;
        data.state = 0;

        state.order_list.unshift(data)
        state.showLoading = false;
    },
    [types.ORDER_UPDATED](state, param) {
        for (var i = 0; i < state.order_list.length; i++) {
            if (state.order_list[i].order_id === param.order_id) {
                if (!param.hasOwnproperty('value')) {
                    for (var p in param) {
                        state.order_list[i][p] = param[p];
                    }
                }
                else {
                    state.order_list[i].state = param.value;
                }
                break;
            }
        }
        state.showLoading = false;
    },
    [types.ORDER_DELETED](state, param) {
        for (var i = 0; i < state.order_list.length; i++) {
            if (state.order_list[i].order_id === param) {
                state.order_list.splice(i, 1);
                break;
            }
        }
        state.showLoading = false;
    }
}

export default {
    state,
    actions,
    mutations
}
